package com.xwd.service.impl;

import com.xwd.dao.MiddleDao;
import com.xwd.dao.RoleDao;
import com.xwd.dao.impl.MiddleDaoImpl;
import com.xwd.dao.impl.RoleDaoImpl;
import com.xwd.domain.Menu;
import com.xwd.domain.Role;
import com.xwd.service.RoleService;

import java.util.List;

public class RoleServiceImpl implements RoleService {

    private RoleDao roleDao = new RoleDaoImpl();
    private MiddleDao middleDao = new MiddleDaoImpl();

    @Override
    public List<Role> getRoleNameList() {
        return roleDao.getRoleNameList();
    }

    @Override
    public List<Role> getRoleList(int pageIndex, int pageSize) {
        return roleDao.getRoleList(pageIndex,pageSize);
    }

    @Override
    public int getTotal() {
        return roleDao.getTotal();
    }

    @Override
    public int insertRole(String roleName, String  roleState, String[] menuIds) {
        int k=0;
        try {
            //插入角色表
            Role role = new Role();
            role.setRoleName(roleName);
            role.setRoleState(Integer.parseInt(roleState));
            int key = roleDao.insertRole(role);
            //插入角色表
            int i = middleDao.insertRoleMenu(key, menuIds);
            k=1;
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return k;
    }

    @Override
    public Role getRoleByid(int roleId) {
        return roleDao.getRoleByid(roleId);
    }

    @Override
    public List<Menu> getMenuListByid(int roleId) {
        return middleDao.findMenuListByRoleId(roleId);
    }

    @Override
    public int updateRole(int roleId, String roleName, String roleState) {
        return roleDao.updateRole(roleId,roleName,roleState);
    }

    @Override
    public int updateMenu(int roleId, String[] menuLists) {
        //删除middle表原来的角色对应的菜单信息
        middleDao.deletebyRoleId(roleId);
        //添加新的middle对应信息
        int i = middleDao.insertRoleMenu(roleId, menuLists);
        return i;
    }

    @Override
    public int delete(int roleId) {
        //删除角色基本信息
        int i = roleDao.delete(roleId);
        //删除middle中间表信息
        middleDao.deletebyRoleId(roleId);
        return i;
    }
}
